package practices;

import java.util.Scanner;

public class InvalidAgeExceptionDemo extends Exception {
    public InvalidAgeExceptionDemo(String message) {
        super(message);
    }
}

class InvalidAgeException {
    public static void validateAge(int age) throws InvalidAgeExceptionDemo{
        if (age < 0 || age > 120) {
            throw new InvalidAgeExceptionDemo("年龄必须在0到120之间！");
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年龄：");

        try {
            int age = Integer.parseInt(sc.nextLine());
            validateAge(age);
            System.out.println("年龄为：" + age);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            System.out.println("输入的年龄无效，请重新输入");
        }catch (InvalidAgeExceptionDemo e) {
            System.out.println("错误:" + e.getMessage());
        }finally {
            sc.close();
        }
    }
}